package com.example.surveyapplication;

import help.Define;
import help.Utility;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import bus.AccountBUS;
import dto.AccountDTO;

public class SignUpActivity extends Activity implements OnClickListener {
	Button btnSignUp;
	TextView lblTerm;
	EditText txtEmail;
	EditText txtPassword;
	EditText txtConfirmPassword;
	EditText txtFirstName;
	EditText txtLastName;
	CheckBox ckbTerm;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sign_up);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		
		initComponents();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.sign_up, menu);
		return true;
	}
	
	private void initComponents() {
		btnSignUp = (Button)findViewById(R.id.btnSignUp_SignUp);
		lblTerm = (TextView)findViewById(R.id.lblTerm_SignUp);
		txtEmail = (EditText)findViewById(R.id.txtEmail_SignUp);
		txtPassword = (EditText)findViewById(R.id.txtPassword_SignUp);
		txtConfirmPassword = (EditText)findViewById(R.id.txtConfirmPassword_SignUp);
		txtFirstName = (EditText)findViewById(R.id.txtFirstname_SignUp);
		txtLastName = (EditText)findViewById(R.id.txtLastname_SignUp);
		ckbTerm = (CheckBox)findViewById(R.id.ckbTerm_SignUp);
		
		btnSignUp.setOnClickListener(this);
		lblTerm.setOnClickListener(this);
	}

	public void onClick(View view) {
		Intent intent;
		switch (view.getId()) {
		case R.id.btnSignUp_SignUp:
			String email = txtEmail.getText().toString();
			String password = txtPassword.getText().toString();
			String confirm = txtConfirmPassword.getText().toString();
			String firstname = txtFirstName.getText().toString();
			String lastname = txtLastName.getText().toString();
			boolean isCheckTerm = ckbTerm.isChecked();
			
			switch (Utility.signUp(email, password, confirm, firstname, lastname, isCheckTerm)) {
			case Define.kEmpty:
				showAlert("Please check again.");
				break;
			case Define.kExist:
				showAlert("Email is exist.");
				break;
			case Define.kUnCheck:
				showAlert("Uncheck the Terms and Conditions.");
				break;
			case Define.kUnMatch:
				showAlert("Confirm password is wrong.");
				break;
			case Define.kFail:
				showAlert("Sorry, application is busy.");
				break;
			case Define.kSuccess:
				showAlert("Sign up is success.");
				super.onBackPressed();
				break;
			default:
				break;
			}
			break;
		case R.id.lblTerm_SignUp:
			intent = new Intent(this, TermActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}
	
	private void showAlert(String message) {
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle("");
		alertDialog.setMessage(message);
		alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				}
				});
		alertDialog.show();
	}
}
